package com.service.impl;

import com.mapper.UserMapper;
import com.pojo.User;
import com.service.UserService;
import com.util.SqlSessionFactoryUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserServiceImpl implements UserService {

    //SqlSessionFactory工厂对象
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public User selectByUsername(String username) {
        //获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取UserMapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //获取User对象
        User user = userMapper.selectByUsername(username);

        sqlSession.close();

        return user;
    }

    @Override
    public User select(String username, String password){
        //获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取UserMapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //获取User对象
        User user = userMapper.select(username, password);

        sqlSession.close();

        return user;
    }

    /**
     * 根据username查找work_id
     * @param username
     * @return
     */
    @Override
    public String selectWorkIdByUsername(String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectByUsername(username);
        String workId = user.getWorkId();

        sqlSession.close();
        return workId;
    }
}
